<?php
namespace app\index\controller;
use think\Controller;
class Index extends Controller
{
	/**
	 * 首页
	 * @return [type] [description]
	 */
    public function index()
    {
      // $a=time();
      // echo $a;
      // echo "<br>";
      // echo date('y/m/d',$a);
      // exit;
      $user=session('user','','index');
    	//查找art表中有关食物(parent_id=3)的
    	$foot=model('art')->where('cat_id',3)->limit(3)->select();
    	$spoot=model('art')->where('cat_id',4)->limit(6)->select();
    	// var_dump($foot);exit;
        return $this->fetch('',['foot'=>$foot,'spoot'=>$spoot,'user'=>$user]);
    }
    /**
     * 列表
     * @return [type] [description]
     */
    public function li(){
  	
    	$parent_id=input('parent_id');
    	// echo $parent_id;
    	$list=model('art')->where('cat_id',$parent_id)->paginate(5);
    	$name=model('art')->where('cat_id',$parent_id)->find();
    	// var_dump($name);exit;
    	$cat_name=model('cat')->where('cat_id',$name['cat_id'])->value('cat_name');
    	return $this->fetch('',['list'=>$list,'cat_name'=>$cat_name]);
    }
    /**
     * 详情
     */
    public function detail(){
      $art_id=input('art_id');
      // echo $art_id;
      $list=model('art')->getArtById($art_id);
      $list1=model('art')->getMes($art_id);
      // var_dump($list1);exit;

      $count=count($list1);
    	return $this->fetch('',['list'=>$list,'list1'=>$list1,'count'=>$count]);
    }
    /**
     * 留言
     */
    public function message(){
      if(request()->isPost()){
        if(empty(session('user','','index')))
          {
            $this->error('您还没有登录，请登陆后发言','login/login');
          }else{
            
            $data=[
              'writer'=>session('user','','index')['username'],
              'content'=>input('post.content'),
              'create_time'=>time(),
              'art_id'=>input('id'),
            ];
           // var_dump($data);exit;
            if(model('message')->add($data)){
             $url=getenv('HTTP_REFERER');
             
              $this->success('留言发表成功',$url);
              
            }
          }
      }
    }
}
